package com.luoye.lj.platform.controller;

import com.luoye.lj.platform.model.domain.AuthInfo;
import com.luoye.lj.platform.model.domain.AuthInfoDetailCollection;
import com.luoye.lj.platform.model.domain.AuthInfoSearchCondition;
import com.luoye.lj.platform.model.domain.SearchParams;
import com.luoye.lj.platform.service.IAuthService;
import com.luoye.lj.util.mvc.annotation.OperateLog;
import com.luoye.lj.util.mvc.data.SimpleListResult;
import com.luoye.lj.util.mvc.data.SimpleResponseBody;
import com.luoye.lj.util.mvc.log.DefaultDescriptionGenerator;
import com.luoye.lj.util.mvc.util.PageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * Created by wei on 2015/12/08.
 */
@RequestMapping("/system/auth")
@Controller
public class AuthController {
    Logger logger= LoggerFactory.getLogger(AuthController.class);
    @Resource
    private IAuthService service;

    @RequestMapping("/list")
    public String tolist(Model model){
        model.addAttribute("title","权限管理");
        return "system/auth/auth_list";
    }

    @RequestMapping("/search")
    @ResponseBody
    public SimpleListResult search(AuthInfoSearchCondition authsearch ,
                                   @RequestParam(value = "page", defaultValue = "1")int page,
                                   @RequestParam(value = "max", defaultValue = "10")int max){
        SimpleListResult slr = new SimpleListResult();
        int offset = (page - 1) * max;
        logger.debug("offset->" + offset + ",max->" + max);
        SearchParams searchParams = new SearchParams();
        searchParams.setOffset(offset);
        searchParams.setMax(max);

        authsearch.setSearchParams(searchParams);
        AuthInfoDetailCollection authcollection = null;
        authsearch.setDelmark(0);
        try {
            authcollection = service.search(authsearch);
        }catch(Exception e){
            logger.warn(e.getMessage(),e);
        }

        if(authcollection != null) {
            slr.setRows(authcollection.getDetailList());
            slr.setCurrentPage(page);
            slr.setResults(authcollection.getTotalCount());
            slr.setPages(PageService.countPages(max, slr.getResults()));
        }
        return slr;
    }
    @RequestMapping(value = "/del",method = RequestMethod.POST)
    @ResponseBody
    @OperateLog(value = "系统级别的权限删除",descriptionGenerator = DefaultDescriptionGenerator.class)
    public boolean del(long id){
        try {
            return service.delAuth(id);
        }catch(Exception e){
            logger.warn(e.getMessage(),e);
        }
        return false;
    }
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    @ResponseBody
    @OperateLog(value = "系统级别的权限修改",descriptionGenerator = DefaultDescriptionGenerator.class)
    public SimpleResponseBody edit(AuthInfo authInfo){
        authInfo.setAuthname(authInfo.getAuthname().trim());
        authInfo.setDescription(authInfo.getDescription().trim());
        SimpleResponseBody srb = new SimpleResponseBody();
        try {
            service.modifyAuth(authInfo);
            srb.setOk(true);
            return srb;
        }catch(Exception  e){
            logger.warn(e.getMessage(),e);
            srb.setMsg(e.getMessage());
            srb.setOk(false);
            return srb;
        }
    }
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    @OperateLog(value = "系统级别的权限添加",descriptionGenerator = DefaultDescriptionGenerator.class)
    public SimpleResponseBody addAuth(AuthInfo authInfo){
        authInfo.setAuthname(authInfo.getAuthname().trim());
        authInfo.setDescription(authInfo.getDescription().trim());
        SimpleResponseBody srb = new SimpleResponseBody();
        try {
            service.addAuth(authInfo);
            srb.setOk(true);
            return srb;
        }catch(Exception e){
            logger.warn(e.getMessage(),e);
            srb.setOk(false);
            srb.setMsg(e.getMessage());
            return srb;
        }
    }

}
